using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Collections.Generic;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly: WebResource("Ajax.Logging.Scripts.PanelListener.js", "text/javascript")]


namespace Ajax.Logging
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [DefaultProperty("Panel")]
    public class PanelListener : BaseListener
    {
        private string _panelId = string.Empty;

        [IDReferenceProperty(typeof(Panel))]
        [DefaultValue("")]
        [Category("Behavior")]
        public string Panel
        {
            [DebuggerStepThrough()]
            get
            {
                return _panelId;
            }
            [DebuggerStepThrough()]
            set
            {
                if (_panelId != value)
                {
                    _panelId = value;
                }
            }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override string CreateScript
        {
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters")]
            [DebuggerStepThrough()]
            get
            {
                if (string.IsNullOrEmpty(Panel))
                {
                    throw new InvalidOperationException("Panel must be specified.");
                }

                Panel pnl = Owner.NamingContainer.FindControl(Panel) as Panel;

                if (pnl == null)
                {
                    throw new InvalidOperationException("Unable to find the specified panel.");
                }

                string script = string.Format(System.Globalization.CultureInfo.InvariantCulture, "Ajax.Logging.ExceptionManager.getInstance().addListener(new {0}('<br/>', '{1}'));", this.GetType().FullName, pnl.ClientID);

                return script;
            }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override IEnumerable<ScriptReference> ScriptReferences
        {
            [DebuggerStepThrough()]
            get
            {
                return new ScriptReference[] { new ScriptReference(Owner.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Ajax.Logging.Scripts.PanelListener.js")) };
            }
        }
    }
}
